{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This cheatsheet is modified from Derek Banas'. You can find the youtube tutorial and the original cheatsheet below.\n",
    "\n",
    "Youtube tutorial (43 min): https://www.youtube.com/watch?v=N4mEzFDjqtA\n",
    "\n",
    "Original cheatsheet: http://www.newthinktank.com/2014/11/python-programming/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python Notebook\n",
    "\n",
    "### Useful keyboard shortcuts (under \"Help\" menu)\n",
    "* [ESC] command mode (vs edit mode)\n",
    "* [A] / [B] insert [A]bove/[B]elow\n",
    "* [DD] delete cell\n",
    "* [Sh-M] [Ctrl-Sh-Minus] merge (command mode) / split (edit mode) cells below\n",
    "* [M] / [Y] to [M]arkdown/code[Y] \n",
    "* [Ctrl-Enter] / [Sh-Enter] run the cell (and go below)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# import is used to make specialty functions available\n",
    "# These are called modules\n",
    "import random\n",
    "# import random as rnd\n",
    "import sys\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n"
     ]
    }
   ],
   "source": [
    "# Hello world is just one line of code\n",
    "# print() outputs data to the screen\n",
    "print(\"Hello World\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nThis is a multi-line comment\\n'"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# this is a single line comment\n",
    "'''\n",
    "This is a multi-line comment\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jaehyuk\n"
     ]
    }
   ],
   "source": [
    "# A variable is a place to store values\n",
    "# Its name is like a label for that value\n",
    "name = \"Jaehyuk\"\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "888\n"
     ]
    }
   ],
   "source": [
    "# There are 5 data types Numbers, Strings, List, Tuple, Dictionary\n",
    "# You can store any of them in the same variable\n",
    "\n",
    "number = 888\n",
    "print(number)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The arithmetic operators +, -, *, /, %, **, //"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 + 2 = 7\n",
      "5 - 2 = 3\n",
      "5 * 2 = 10\n",
      "5 / 2 = 2.5\n",
      "5 % 2 = 1\n",
      "5 ** 2 = 25\n",
      "5 // 2 = 2\n"
     ]
    }
   ],
   "source": [
    "print(\"5 + 2 =\", 5+2)\n",
    "print(\"5 - 2 =\", 5-2)\n",
    "print(\"5 * 2 =\", 5*2)\n",
    "print(\"5 / 2 =\", 5/2)\n",
    "print(\"5 % 2 =\", 5%2)\n",
    "print(\"5 ** 2 =\", 5**2) # ** Exponential calculation\n",
    "print(\"5 // 2 =\", 5//2) # // Floor Division"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 + 2 - 3 * 2 = -3\n",
      "(1 + 2 - 3) * 2 = 0\n"
     ]
    }
   ],
   "source": [
    "# Order of Operation states * and / is performed before + and -\n",
    "\n",
    "print(\"1 + 2 - 3 * 2 =\", 1 + 2 - 3 * 2)\n",
    "print(\"(1 + 2 - 3) * 2 =\", (1 + 2 - 3) * 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"Always remember your unique, just\n",
      "like everyone else\" \n"
     ]
    }
   ],
   "source": [
    "# A string is a string of characters surrounded by \" or '\n",
    "# If you must use a \" or ' between the same quote escape it with \\\n",
    "quote = \"\\\"Always remember your unique,\"\n",
    "# A multi-line quote\n",
    "multi_line_quote = ''' just\n",
    "like everyone else\" '''\n",
    "\n",
    "print(quote + multi_line_quote)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I like the quote \"Always remember your unique,  just\n",
      "like everyone else\" \n",
      "10 / 20\n"
     ]
    }
   ],
   "source": [
    "# To embed a string in output use %s\n",
    "print(\"{} {} {}\".format('I like the quote', quote, multi_line_quote))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I don't like newlines\n"
     ]
    }
   ],
   "source": [
    "# To keep from printing newlines use end=\"\"\n",
    "print(\"I don't like \",end=\"\")\n",
    "print(\"newlines\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# You can print a string multiple times with *\n",
    "print('\\n' * 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 / 20\n",
      "two / one\n",
      "3.141593\n",
      "0.33333 3.14159\n",
      "0.33333 3.14159\n"
     ]
    }
   ],
   "source": [
    "# Printing with formsts\n",
    "print(\"{} / {}\".format(10,20))\n",
    "\n",
    "print(\"{1} / {0}\".format('one','two'))\n",
    "\n",
    "print('{:f}'.format(3.141592653589793))\n",
    "\n",
    "print('{1:0.5f} {0:0.5f}'.format(3.141592653589793, 0.33333333333))\n",
    "\n",
    "print('{onethird:0.5f} {pi:0.5f}'.format(pi=3.141592653589793, onethird=0.33333333333))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The first item is Tomatoes\n"
     ]
    }
   ],
   "source": [
    "# A list allows you to create a list of values and manipulate them\n",
    "# Each value has an index with the first one starting at 0\n",
    "\n",
    "grocery_list = ['Juice', 'Tomatoes', 'Potatoes', 'Bananas']\n",
    "print('The first item is', grocery_list[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Green Juice', 'Tomatoes', 'Potatoes', 'Bananas']\n"
     ]
    }
   ],
   "source": [
    "# You can change the value stored in a list box\n",
    "grocery_list[0] = \"Green Juice\"\n",
    "print(grocery_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tomatoes', 'Potatoes']\n"
     ]
    }
   ],
   "source": [
    "# You can get a subset of the list with [min:up to but not including max]\n",
    "\n",
    "print(grocery_list[1:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['Wash Car', 'Pick up Kids', 'Cash Check'], ['Green Juice', 'Tomatoes', 'Potatoes', 'Bananas']]\n"
     ]
    }
   ],
   "source": [
    "# You can put any data type in a a list including a list\n",
    "other_events = ['Wash Car', 'Pick up Kids', 'Cash Check']\n",
    "to_do_list = [other_events, grocery_list]\n",
    "\n",
    "print(to_do_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tomatoes\n"
     ]
    }
   ],
   "source": [
    "# Get the second item in the second list (Boxes inside of boxes)\n",
    "print(to_do_list[1][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['Wash Car', 'Pick up Kids', 'Cash Check'], ['Green Juice', 'Tomatoes', 'Potatoes', 'Bananas', 'Onions']]\n"
     ]
    }
   ],
   "source": [
    "# You add values using append\n",
    "grocery_list.append('Onions')\n",
    "print(to_do_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Green Juice', 'Pickle', 'Tomatoes', 'Potatoes', 'Bananas', 'Onions']\n"
     ]
    }
   ],
   "source": [
    "# Insert item at given index\n",
    "grocery_list.insert(1, \"Pickle\")\n",
    "print(grocery_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Green Juice', 'Tomatoes', 'Potatoes', 'Bananas', 'Onions']\n"
     ]
    }
   ],
   "source": [
    "# Remove item from list\n",
    "grocery_list.remove(\"Pickle\")\n",
    "print(grocery_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Bananas', 'Green Juice', 'Onions', 'Potatoes', 'Tomatoes']\n"
     ]
    }
   ],
   "source": [
    "# Sorts items in list\n",
    "grocery_list.sort()\n",
    "print(grocery_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tomatoes', 'Potatoes', 'Onions', 'Green Juice', 'Bananas']\n"
     ]
    }
   ],
   "source": [
    "# Reverse sort items in list\n",
    "grocery_list.reverse()\n",
    "print(grocery_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['Wash Car', 'Pick up Kids', 'Cash Check'], ['Tomatoes', 'Potatoes', 'Onions', 'Green Juice']]\n"
     ]
    }
   ],
   "source": [
    "# del deletes an item at specified index\n",
    "del grocery_list[4]\n",
    "print(to_do_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Wash Car', 'Pick up Kids', 'Cash Check', 'Tomatoes', 'Potatoes', 'Onions', 'Green Juice']\n",
      "7\n",
      "Wash Car\n",
      "Cash Check\n"
     ]
    }
   ],
   "source": [
    "# We can combine lists with a +\n",
    "to_do_list = other_events + grocery_list\n",
    "print(to_do_list)\n",
    "\n",
    "# Get length of list\n",
    "print(len(to_do_list))\n",
    "\n",
    "# Get the max item in list\n",
    "print(max(to_do_list))\n",
    "\n",
    "# Get the minimum item in list\n",
    "print(min(to_do_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tuples\n",
    "* Values in a tuple can't change like lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pi_tuple = (3, 1, 4, 1, 5, 9)\n",
    "\n",
    "# Convert tuple into a list\n",
    "new_tuple = list(pi_tuple)\n",
    "\n",
    "# Convert a list into a tuple\n",
    "# new_list = tuple(grocery_list)\n",
    "\n",
    "# tuples also have len(tuple), min(tuple) and max(tuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dictionary or Map\n",
    "* Made up of values with a unique key for each value\n",
    "* Similar to lists, but you can't join dicts with a +"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Pied Piper': 'Thomas Peterson', 'Weather Wizard': 'Mark Mardon', 'Captain Cold': 'Leonard Snart', 'Fiddler': 'Isaac Bowin', 'Mirror Master': 'Sam Scudder'}\n",
      "Leonard Snart\n"
     ]
    }
   ],
   "source": [
    "super_villains = {'Fiddler' : 'Isaac Bowin',\n",
    "                'Captain Cold' : 'Leonard Snart',\n",
    "                'Weather Wizard' : 'Mark Mardon',\n",
    "                'Mirror Master' : 'Sam Scudder',\n",
    "                'Pied Piper' : 'Thomas Peterson'}\n",
    "print(super_villains)\n",
    "print(super_villains['Captain Cold'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Delete an entry\n",
    "del super_villains['Fiddler']\n",
    "print(super_villains)\n",
    "\n",
    "# Replace a value\n",
    "super_villains['Pied Piper'] = 'Hartley Rathaway'\n",
    "\n",
    "# Print the number of items in the dictionary\n",
    "print(len(super_villains))\n",
    "\n",
    "# Get the value for the passed key\n",
    "print(super_villains.get(\"Pied Piper\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['Pied Piper', 'Weather Wizard', 'Captain Cold', 'Fiddler', 'Mirror Master'])\n",
      "dict_values(['Thomas Peterson', 'Mark Mardon', 'Leonard Snart', 'Isaac Bowin', 'Sam Scudder'])\n"
     ]
    }
   ],
   "source": [
    "# Get a list of dictionary keys\n",
    "print(super_villains.keys())\n",
    "\n",
    "# Get a list of dictionary values\n",
    "print(super_villains.values())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CONDITIONALS\n",
    "* The if, else and elif statements are used to perform different\n",
    "* actions based off of conditions\n",
    "* Comparison Operators : ==, !=, >, <, >=, <="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are old enough to drive\n"
     ]
    }
   ],
   "source": [
    "# The if statement will execute code if a condition is met\n",
    "# White space is used to group blocks of code in Python\n",
    "# Use the same number of proceeding spaces for blocks of code\n",
    "\n",
    "age = 30\n",
    "if age > 16 :\n",
    "    print('You are old enough to drive')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Use an if statement if you want to execute different code regardless\n",
    "# of whether the condition ws met or not\n",
    "\n",
    "if age > 16 :\n",
    "    print('You are old enough to drive')\n",
    "else :\n",
    "    print('You are not old enough to drive')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# If you want to check for multiple conditions use elif\n",
    "# If the first matches it won't check other conditions that follow\n",
    "\n",
    "if age >= 21 :\n",
    "    print('You are old enough to drive a tractor trailer')\n",
    "elif age >= 16:\n",
    "    print('You are old enough to drive a car')\n",
    "else :\n",
    "    print('You are not old enough to drive')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You get a birthday party yeah\n"
     ]
    }
   ],
   "source": [
    "# You can combine conditions with logical operators\n",
    "# Logical Operators : and, or, not\n",
    "\n",
    "if ((age >= 1) and (age <= 18)):\n",
    "    print(\"You get a birthday party\")\n",
    "elif (age == 21) or (age >= 65):\n",
    "    print(\"You get a birthday party\")\n",
    "elif not(age == 30):  # !(age == 30) doesn't work\n",
    "    print(\"You don't get a birthday party\")\n",
    "else:\n",
    "    print(\"You get a birthday party yeah\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FOR LOOPS\n",
    "* Allows you to perform an action a set number of times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0  1  2  3  4  5  6  7  8  9  \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Range performs the action 10 times 0 - 9\n",
    "for x in range(0, 10):\n",
    "    print(x , ' ', end=\"\")\n",
    "\n",
    "print('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Juice\n",
      "Tomatoes\n",
      "Potatoes\n",
      "Bananas\n"
     ]
    }
   ],
   "source": [
    "# You can use for loops to cycle through a list\n",
    "grocery_list = ['Juice', 'Tomatoes', 'Potatoes', 'Bananas']\n",
    "\n",
    "for y in grocery_list:\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "# You can also define a list of numbers to cycle through\n",
    "for x in [2,4,6,8,10]:\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "10\n",
      "20\n",
      "30\n",
      "100\n",
      "200\n",
      "300\n",
      "1\n",
      "2\n",
      "3\n",
      "10\n",
      "20\n",
      "30\n",
      "100\n",
      "200\n",
      "300\n"
     ]
    }
   ],
   "source": [
    "# You can double up for loops to cycle through lists\n",
    "num_list =[[1,2,3],[10,20,30],[100,200,300]];\n",
    "\n",
    "for x in range(0,3):\n",
    "    for y in range(0,3):\n",
    "        print(num_list[x][y])\n",
    "\n",
    "# Instead you can do        \n",
    "for x in num_list:\n",
    "    for y in x:\n",
    "        print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## WHILE LOOPS -------------\n",
    "* While loops are used when you don't know ahead of time how many times you'll have to loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "78\n",
      "67\n",
      "86\n",
      "15\n"
     ]
    }
   ],
   "source": [
    "random_num = 50\n",
    "\n",
    "while (random_num > 20):\n",
    "    random_num = random.randrange(0,100)\n",
    "    print(random_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "# An iterator for a while loop is defined before the loop\n",
    "i = 0;\n",
    "while (i <= 20):\n",
    "    if(i%2 == 0):\n",
    "        print(i)\n",
    "    elif(i == 9):\n",
    "        # Forces the loop to end all together\n",
    "        break\n",
    "    else:\n",
    "        # Shorthand for i = i + 1\n",
    "        i += 1\n",
    "        # Skips to the next iteration of the loop\n",
    "        continue\n",
    "\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FUNCTIONS\n",
    "* Functions allow you to reuse and write readable code\n",
    "* Type def (define), function name and parameters it receives\n",
    "* return is used to return something to the caller of the function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "def addNumbers(fNum, sNum):\n",
    "    sumNum = fNum + sNum\n",
    "    return sumNum\n",
    "\n",
    "print(addNumbers(1, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Can't get the value of sumNum because it was created in a function\n",
    "# It is said to be out of scope\n",
    "#print(sumNum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-3\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# If you define a variable outside of the function it works every place\n",
    "newNum = 0;\n",
    "def subNumbers(fNum, sNum):\n",
    "    newNum = fNum - sNum\n",
    "    return newNum\n",
    "\n",
    "print(subNumbers(1, 4))\n",
    "print(newNum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# USER INPUT -------------\n",
    "print('What is your name?')\n",
    "\n",
    "# Stores everything typed up until ENTER\n",
    "#name = sys.stdin.readline()\n",
    "\n",
    "#print('Hello', name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## STRINGS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'll\n",
      "Floor\n",
      "I'll catch you if you fall - The \n",
      "I'll be there\n"
     ]
    }
   ],
   "source": [
    "# A string is a series of characters surrounded by ' or \"\n",
    "long_string = \"I'll catch you if you fall - The Floor\"\n",
    "\n",
    "# Retrieve the first 4 characters\n",
    "print(long_string[0:4])\n",
    "\n",
    "# Get the last 5 characters\n",
    "print(long_string[-5:])\n",
    "\n",
    "# Everything up to the last 5 characters\n",
    "print(long_string[:-5])\n",
    "\n",
    "# Concatenate part of a string to another\n",
    "print(long_string[:4] + \" be there\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X is my favorite letter and my number 1 number is 0.14000\n",
      "I'll catch you if you fall - the floor\n",
      "33\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# String formatting <-- This is old format\n",
    "print(\"%c is my %s letter and my number %d number is %.5f\" % ('X', 'favorite', 1, .14))\n",
    "\n",
    "# Capitalizes the first letter\n",
    "print(long_string.capitalize())\n",
    "\n",
    "# Returns the index of the start of the string\n",
    "# case sensitive\n",
    "print(long_string.find(\"Floor\"))\n",
    "\n",
    "# Returns true if all characters are letters ' isn't a letter\n",
    "print(long_string.isalpha())\n",
    "\n",
    "# Returns true if all characters are numbers\n",
    "print(long_string.isalnum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38\n",
      "I'll catch you if you fall - The Ground\n",
      "I'll catch you if you fall - The Floor\n",
      "[\"I'll\", 'catch', 'you', 'if', 'you', 'fall', '-', 'The', 'Floor']\n"
     ]
    }
   ],
   "source": [
    "# Returns the string length\n",
    "print(len(long_string))\n",
    "\n",
    "# Replace the first word with the second (Add a number to replace more)\n",
    "print(long_string.replace(\"Floor\", \"Ground\"))\n",
    "\n",
    "# Remove white space from front and end\n",
    "print(long_string.strip())\n",
    "\n",
    "# Split a string into a list based on the delimiter you provide\n",
    "quote_list = long_string.split(\" \")\n",
    "print(quote_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FILE I/O"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wb\n",
      "test.txt\n",
      "Write me to the file\n",
      "\n"
     ]
    },
    {
     "ename": "PermissionError",
     "evalue": "[WinError 32] The process cannot access the file because it is being used by another process: 'test.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mPermissionError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-15-33c5907ccc86>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     23\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     24\u001b[0m \u001b[1;31m# Delete the file\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"test.txt\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mPermissionError\u001b[0m: [WinError 32] The process cannot access the file because it is being used by another process: 'test.txt'"
     ]
    }
   ],
   "source": [
    "# Overwrite or create a file for writing\n",
    "test_file = open(\"test.txt\", \"wb\")\n",
    "\n",
    "# Get the file mode used\n",
    "print(test_file.mode)\n",
    "\n",
    "# Get the files name\n",
    "print(test_file.name)\n",
    "\n",
    "# Write text to a file with a newline\n",
    "test_file.write(bytes(\"Write me to the file\\n\", 'UTF-8'))\n",
    "\n",
    "# Close the file\n",
    "test_file.close()\n",
    "\n",
    "# Opens a file for reading and writing\n",
    "test_file = open(\"test.txt\", \"r+\")\n",
    "\n",
    "# Read text from the file\n",
    "text_in_file = test_file.read()\n",
    "\n",
    "print(text_in_file)\n",
    "\n",
    "# Delete the file\n",
    "os.remove(\"test.txt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CLASSES AND OBJECTS\n",
    "* The concept of OOP allows us to model real world things using code\n",
    "* Every object has attributes (color, height, weight) which are object variables\n",
    "* Every object has abilities (walk, talk, eat) which are object functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Animal:\n",
    "    # None signifies the lack of a value\n",
    "    # You can make a variable private by starting it with __\n",
    "    __name = None\n",
    "    __height = None\n",
    "    __weight = None\n",
    "    __sound = None\n",
    "\n",
    "    # The constructor is called to set up or initialize an object\n",
    "    # self allows an object to refer to itself inside of the class\n",
    "    def __init__(self, name, height, weight, sound):\n",
    "        self.__name = name\n",
    "        self.__height = height\n",
    "        self.__weight = weight\n",
    "        self.__sound = sound\n",
    "\n",
    "    def set_name(self, name):\n",
    "        self.__name = name\n",
    "\n",
    "    def set_height(self, height):\n",
    "        self.__height = height\n",
    "\n",
    "    def set_weight(self, height):\n",
    "        self.__height = height\n",
    "\n",
    "    def set_sound(self, sound):\n",
    "        self.__sound = sound\n",
    "\n",
    "    def get_name(self):\n",
    "        return self.__name\n",
    "\n",
    "    def get_height(self):\n",
    "        return str(self.__height)\n",
    "\n",
    "    def get_weight(self):\n",
    "        return str(self.__weight)\n",
    "\n",
    "    def get_sound(self):\n",
    "        return self.__sound\n",
    "\n",
    "    def get_type(self):\n",
    "        print(\"Animal\")\n",
    "\n",
    "    def toString(self):\n",
    "        return \"{} is {} cm tall and {} kilograms and says {}\".format(self.__name, self.__height, self.__weight, self.__sound)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_Animal__height': 33, '_Animal__weight': 10, '_Animal__name': 'Whiskers', '_Animal__sound': 'Meow'}\n",
      "Whiskers is 33 cm tall and 10 kilograms and says Meow\n"
     ]
    }
   ],
   "source": [
    "# How to create a Animal object\n",
    "cat = Animal('Whiskers', 33, 10, 'Meow')\n",
    "print(cat.toString())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# You can't access this value directly because it is private\n",
    "#print(cat.__name)\n",
    "print(cat.__dict__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Spot is 53 cm tall and 27 kilograms and says Ruff. His owner is Derek\n",
      "Animal\n",
      "Dog\n",
      "RuffRuffRuffRuff\n"
     ]
    }
   ],
   "source": [
    "# INHERITANCE -------------\n",
    "# You can inherit all of the variables and methods from another class\n",
    "\n",
    "class Dog(Animal):\n",
    "    __owner = None\n",
    "\n",
    "    def __init__(self, name, height, weight, sound, owner):\n",
    "        self.__owner = owner\n",
    "        self.__animal_type = None\n",
    "\n",
    "        # How to call the super class constructor\n",
    "        super(Dog, self).__init__(name, height, weight, sound)\n",
    "\n",
    "    def set_owner(self, owner):\n",
    "        self.__owner = owner\n",
    "\n",
    "    def get_owner(self):\n",
    "        return self.__owner\n",
    "\n",
    "    def get_type(self):\n",
    "        print (\"Dog\")\n",
    "\n",
    "    # We can overwrite functions in the super class\n",
    "    def toString(self):\n",
    "        return \"{} is {} cm tall and {} kilograms and says {}. His owner is {}\".format(self.get_name(), self.get_height(), self.get_weight(), self.get_sound(), self.__owner)\n",
    "\n",
    "    # You don't have to require attributes to be sent\n",
    "    # This allows for method overloading\n",
    "    def multiple_sounds(self, how_many=None):\n",
    "        if how_many is None:\n",
    "            print(self.get_sound)\n",
    "        else:\n",
    "            print(self.get_sound() * how_many)\n",
    "\n",
    "spot = Dog(\"Spot\", 53, 27, \"Ruff\", \"Derek\")\n",
    "\n",
    "print(spot.toString())\n",
    "\n",
    "# Polymorphism allows use to refer to objects as their super class\n",
    "# and the correct functions are called automatically\n",
    "\n",
    "class AnimalTesting:\n",
    "    def get_type(self, animal):\n",
    "        animal.get_type()\n",
    "\n",
    "test_animals = AnimalTesting()\n",
    "\n",
    "test_animals.get_type(cat)\n",
    "test_animals.get_type(spot)\n",
    "\n",
    "spot.multiple_sounds(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
